package article

import (
	"encoding/json"
	"forum/internal/common"
	"forum/internal/domain/event/article"
	"github.com/IBM/sarama"
)

type SaramaArticleProducer struct {
	artProducer sarama.SyncProducer
}

func NewSaramaArticleProducer(artProducer sarama.SyncProducer) *SaramaArticleProducer {
	return &SaramaArticleProducer{artProducer: artProducer}
}

func (s SaramaArticleProducer) ProduceReadEvent(evt article.ReadEvent) error {
	val, err := json.Marshal(evt)
	if err != nil {
		return err
	}

	_, _, err = s.artProducer.SendMessage(&sarama.ProducerMessage{
		Topic: common.TopicReadEvent,
		Value: sarama.StringEncoder(val),
	})
	return err
}
